﻿using CloudMusic.Helper;
using CloudMusic.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace CloudMusic.View
{
    /// <summary>
    /// PageLookLive.xaml 的交互逻辑
    /// </summary>
    public partial class PageLookLive : Page
    {
        public PageLookLive()
        {
            InitializeComponent();
            this.SizeChanged += new SizeChangedEventHandler(MainWindow_Resize);
            GetLook();
            
        }
        private void MainWindow_Resize(object sender, System.EventArgs e)
        {
            if (bd.ActualWidth > 800)
            {
                look.Columns = 5;
                return;
            }
            look.Columns = 4;
        }
        private void hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink link = sender as Hyperlink;
            Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
            e.Handled = true;
        }

        private void hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            if (e.Uri != null && string.IsNullOrEmpty(e.Uri.OriginalString) == false)
            {
                string uri = e.Uri.AbsoluteUri;
                Process.Start(new ProcessStartInfo(uri));
                e.Handled = true;

            }
        }
        public void GetLook()
        {
            //var strJosn = CommonHelper.Get("https://api.iplay.163.com/api/livestream/homepage/recommend?limit=100");
            //JObject obj = (JObject)JsonConvert.DeserializeObject(strJosn);
            //string data = obj["data"].ToString();
            //JObject Data = (JObject)JsonConvert.DeserializeObject(data);
            //string itemList = Data["itemList"].ToString();
            try
            {
                Button bt = default;
                LiveDataClass liveDataClass;
                var list = CommonHelper.GetEquipList<LookModel>(1, "https://api.iplay.163.com/api/livestream/homepage/recommend?limit=100");
                for (int i = 0; i < list.data.itemList.Count; i++)
                {
                    liveDataClass = new LiveDataClass();
                    if (list.data.itemList[i].liveData != null)
                    {
                        if (list.data.itemList[i].liveData.activityBadge != null)
                        {
                            liveDataClass.content = list.data.itemList[i].liveData.activityBadge.content;
                            liveDataClass.contentVis = "Visible";
                        }
                        else
                        {
                            liveDataClass.contentVis = "Collapsed";
                        }
                        if (list.data.itemList[i].liveData.extendTag != null)
                        {
                            liveDataClass.extendTag = list.data.itemList[i].liveData.extendTag;
                            liveDataClass.extendTagVis = "Visible";
                        }
                        else
                        {
                            liveDataClass.extendTagVis = "Collapsed";
                        }
                        liveDataClass.liveCoverUrl = list.data.itemList[i].liveData.liveCoverUrl;
                        liveDataClass.liveTitle = list.data.itemList[i].liveData.liveTitle;
                        liveDataClass.nickname = list.data.itemList[i].liveData.userInfo.nickname;
                        liveDataClass.popularity = list.data.itemList[i].liveData.popularity;
                        liveDataClass.avatarUrl = list.data.itemList[i].liveData.userInfo.avatarUrl;
                        liveDataClass.webRoomUrl = list.data.itemList[i].liveData.webRoomUrl;
                        bt = new Button { Style = TryFindResource("LookInfoStyle") as Style, DataContext = liveDataClass };
                        bt.Tag = list.data.itemList[i].liveData.webRoomUrl;
                        bt.Click += Bt_Click;
                        look.Children.Add(bt);
                    }
                }
            }
            catch (System.Exception) { }
           
        }

        private void Bt_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button.Tag != null)
            {
                Process.Start(new ProcessStartInfo(button.Tag.ToString()));
                e.Handled = true;
            }
        }
    }
}
